package com.activitycode.adapter;

// add by liumy  2013 7-19
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.activitycode.adapter.PetTwitterListAdapter.ViewHolder;
import com.activitycode.adapter.PetTwitterListAdapter.ViewHolder;
import com.activitycode.datamodel.model.DetailPetImages;
import com.activitycode.datamodel.twitter.TwitterImage;
import com.activitycode.util.CommConstant;
import com.baidu.platform.comapi.map.v;
import com.duostec.zhuyue.pets.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

public class ImageViewPagerAdapter2 extends PagerAdapter {

	private List<DetailPetImages> list;
	private Context context;

	public final String Tag = "ViewPagerAdapter";
	public int width;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;

	public ImageViewPagerAdapter2(Context ct, ArrayList<DetailPetImages> imageList,
			ImageLoader imageLoader, DisplayImageOptions option) {
		this.context = ct;
		this.list = imageList;
		this.imageLoader = imageLoader;
		this.options = options;

	}

	@Override
	public void destroyItem(View view, int position, Object object) {
		// TODO Auto-generated method stub

		((ViewPager) view).removeView((View) object);
	}

	@Override
	public void finishUpdate(View container) {
		// TODO Auto-generated method stub
	}

	@Override
	public Object instantiateItem(View view, int position) {
	final ViewHolder viewHolder = new ViewHolder();
		View convertView = null;
		convertView = LayoutInflater.from(context).inflate(
				R.layout.adapter_imageviewpager, null);
		ImageView imgView = (ImageView) convertView
				.findViewById(R.id.adapter_imageviewpager_image);
	viewHolder.adapter_imageviewpager_ProgressBar=(ProgressBar) convertView
				.findViewById(R.id.adapter_imageviewpager_ProgressBar);
		// String path = "file://"+list.get(position).getImage_path();
		imageLoader.displayImage(CommConstant.SERVICE_HOST_IP_LAN_IMG
				+ list.get(position).getPetImg(), imgView, options,new ImageLoadingListener() {
					
					@Override
					public void onLoadingStarted(String imageUri, View view) {
						// TODO Auto-generated method stub
						viewHolder.adapter_imageviewpager_ProgressBar.setProgress(0);
						viewHolder.adapter_imageviewpager_ProgressBar.setVisibility(view.VISIBLE);
					}
					
					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
						// TODO Auto-generated method stub
						viewHolder.adapter_imageviewpager_ProgressBar.setVisibility(view.GONE);
					}
					
					@Override
					public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
						// TODO Auto-generated method stub
						viewHolder.adapter_imageviewpager_ProgressBar.setVisibility(view.GONE);

					}
					
					@Override
					public void onLoadingCancelled(String imageUri, View view) {
						// TODO Auto-generated method stub
						viewHolder.adapter_imageviewpager_ProgressBar.setVisibility(view.GONE);

					}
				});

		((ViewPager) view).addView(convertView, 0);

		return convertView;
	}

	@Override
	public int getCount() {
		if (list != null) {
			return list.size();
		}
		return 0;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return (arg0 == arg1);
	}

	@Override
	public int getItemPosition(Object object) {

		return POSITION_NONE;

	}

	final static class ViewHolder {

		ImageView imgView;
ProgressBar adapter_imageviewpager_ProgressBar;
	}

}
